package com.example.game.sdk.framework

import android.app.Activity
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.KeyEvent

//import androidx.activity.OnBackPressedCallback
//import androidx.fragment.app.FragmentActivity

open class GameSdkActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        GameSDK.onCreate(this, savedInstanceState)
        // 注册返回键回调，override fun onBackPressed()已经被弃用
//        onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
    }

//    private val onBackPressedCallback =  object : OnBackPressedCallback(true) {
//        override fun handleOnBackPressed() {
//            if(!GameSDK.onBackPressed(this@GameSdkActivity)){
//                onBackPressedDispatcher.onBackPressed()
//            }
//        }
//    }


    override fun onRestart() {
        super.onRestart()
        GameSDK.onRestart(this)
    }

    override fun onStart() {
        super.onStart()
        GameSDK.onStart(this)
    }

    override fun onResume() {
        super.onResume()
        GameSDK.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        GameSDK.onPause(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        GameSDK.onDestroy(this)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        GameSDK.onConfigurationChanged(this, newConfig)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return GameSDK.onKeyDown(this, keyCode, event) || super.onKeyDown(keyCode, event)
    }

    override fun onBackPressed() {
        if (!GameSDK.onBackPressed(this@GameSdkActivity)) {
            super.onBackPressed()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        GameSDK.onActivityResult(this, requestCode, resultCode, data)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        GameSDK.onRequestPermissionsResult(this, requestCode, permissions, grantResults)
    }
}